package com.youlu.campus.admin.wechat;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.wechat.entity.WechatAppIdAccount;
import com.youlu.campus.wechat.service.WechatAppIdAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 微信商户 appId 账号管理接口
 */
@RestController
@RequestMapping("/wechat/appid")
public class WechatAppIdAccountController {

    @Autowired
    private WechatAppIdAccountService wechatAppIdAccountService;

    /**
     * 创建/修改分账账户信息
     */
    @PostMapping("/save")
    public QResult<WechatAppIdAccount> save(@RequestBody @Valid WechatAppIdAccount wechatAppIdAccount) {
        WechatAppIdAccount savedInfo = wechatAppIdAccountService.save(wechatAppIdAccount);
        return QResult.success(savedInfo);
    }

    @GetMapping("/info")
    public QResult<WechatAppIdAccount> findById(@RequestParam String id) {
        WechatAppIdAccount savedInfo = wechatAppIdAccountService.findById(id);
        return QResult.success(savedInfo);
    }

    /**
     * 根据ID查询分账账户信息
     */
    @GetMapping("/list")
    public QResult<List<WechatAppIdAccount>> listAllByMchId(@RequestParam String merchantId) {
        List<WechatAppIdAccount> wechatAppIdAccounts = wechatAppIdAccountService.listByMerchantId(merchantId);
        return QResult.success(wechatAppIdAccounts);
    }

    /**
     * 删除分账账户信息
     */
    @PostMapping("/delete")
    public QResult<String> delete(@RequestBody WechatAppIdAccount wechatAppIdAccount) {
        wechatAppIdAccountService.deleteById(wechatAppIdAccount.getId());
        return QResult.success(wechatAppIdAccount.getId());
    }
}